package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    //1.新建权限
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    //2.权限分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //1.获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //2.获取每页显示的记录数
        Integer pageSize = queryPageBean.getPageSize();
        //3.获取查询条件
        String queryString = queryPageBean.getQueryString();
        //4.基于mybatis，使用分页插件助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();
        PageResult pageResult = new PageResult(total,rows);
        return pageResult;
    }

    //3.删除权限，根据权限ID去删除
    public void deleteById(Integer id) {
       long count =  permissionDao.findCountByPermissionId(id);
       if(count>0){
           //说明存在关联关系，先删除中间表
           permissionDao.deleteAssocication(id);
       }

       permissionDao.deleteById(id);

    }

    //4.查询权限的所有信息
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }


}
